/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.RoleDAO;
import model.dao.UserDAO;
import model.dao.service.RoleDAOService;
import model.dao.service.UserDAOService;
import model.entities.Role;
import model.entities.User;

/**
 *
 * @author Admin
 */
public class SignupServlet extends HttpServlet {

    private final UserDAOService USER_SERVICE = UserDAO.getInstance();
    private final RoleDAOService ROLE_SERVICE = RoleDAO.getInstance();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            if (request.getCharacterEncoding() == null)
                request.setCharacterEncoding("UTF-8");
            String userName = request.getParameter("userName");
            String pwd = request.getParameter("pwd");
            String fullName = request.getParameter("fullName");
            String day = request.getParameter("birthday_day");
            String month = request.getParameter("birthday_month");
            String year = request.getParameter("birthday_year");
            String bd = year + "/" + month + "/" + day;
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            Date utilBD = df.parse(bd);
            java.sql.Date birthday = new java.sql.Date(utilBD.getTime());
            boolean gender = request.getParameter("gender").equals("male");
            String idCard = request.getParameter("idCard");
            String address = request.getParameter("address");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String strActive = userName + new Date().getTime();
            String idActive = util.MD5.encryptMD5(strActive);
            String password = util.MD5.encryptMD5(pwd);
            Role role = ROLE_SERVICE.getRoleByID(4);
            String imagePath = null;
            if (!USER_SERVICE.checkUser(userName)) {
                User user = new User(1, userName, password, fullName, birthday, gender, idCard, address, email, phone, imagePath, role, idActive, false);
                if (USER_SERVICE.createUser(user)) {
                    util.SendMailSSL.sendMail(email, idActive);
                    request.setAttribute(util.Constants.PAGE, "result");
                    request.setAttribute(util.Constants.PAGE_RESULT, "login");
                    request.removeAttribute(util.Constants.MSG_RESULT);
                    request.setAttribute(util.Constants.MSG_RESULT, "Please, check your mail to the conplete registration!!!");
                    request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                } else {
                    request.setAttribute(util.Constants.PAGE, "result");
                    request.setAttribute(util.Constants.PAGE_RESULT, "signup");
                    request.removeAttribute(util.Constants.MSG_RESULT);
                    request.setAttribute(util.Constants.MSG_RESULT, "You have register failed !!!");
                    request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                }
            } else {
                request.setAttribute(util.Constants.PAGE, "result");
                request.setAttribute(util.Constants.PAGE_RESULT, "signup");
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.setAttribute(util.Constants.MSG_RESULT, "User name is existed !!!");
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
            }
        } catch (ParseException ex) {
            Logger.getLogger(SignupServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
